using System;
using System.Diagnostics;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace CustomVisualizer
{
	public partial class DebugVisualizer
	{
		private readonly IVisualizer _visualizer;

		public DebugVisualizer()
		{
			InitializeComponent();
		}

		[InjectionConstructor]
		public DebugVisualizer(WorkItem rootWorkItem, IVisualizer visualizer)
			: this()
		{
			_visualizer = visualizer;
			//register our listener
			Debug.Listeners.Add(new CustomListener(this));
			//this will show our visualizer in the main visualizer window
			rootWorkItem.Workspaces["MainWorkspace"].Show(this, new SmartPartInfo("Sample Visualizer", string.Empty));
		}

		private sealed class CustomListener : TraceListener
		{
			private readonly DebugVisualizer _debugVisualizer;

			public CustomListener(DebugVisualizer debugVisualizer)
			{
				_debugVisualizer = debugVisualizer;
			}

			public override void Write(string message)
			{
				_debugVisualizer._textBox.AppendText(message);
			}

			public override void WriteLine(string message)
			{
				_debugVisualizer._textBox.AppendText(message);
				_debugVisualizer._textBox.AppendText(Environment.NewLine);
			}
		}
	}
}